﻿Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Windows.Forms

Friend Class ProgressDialog

    Private filename As String = String.Empty
    Private index As Integer
    Private m_pathtoconvert As String
    Public Property PathToConvert() As String
        Get
            Return m_pathtoconvert
        End Get
        Set(ByVal value As String)
            m_pathtoconvert = value
        End Set
    End Property

#Region " Methods "

    Shared Sub ConvertImage(ByVal fileInfo As FileSystemInfo)
        Dim source As Bitmap = TryCast(Bitmap.FromFile(fileInfo.FullName), Bitmap)
        Dim output As New Bitmap(176, 220, PixelFormat.Format16bppRgb565)

        If opts.RotateImages AndAlso source.Width > source.Height Then
            source.RotateFlip(RotateFlipType.Rotate90FlipNone)
        End If
        source.RotateFlip(RotateFlipType.Rotate180FlipX)

        Dim position As Point = Nothing
        Dim targetSize As Size = Nothing
        If opts.Sizing = 1 Then
            position = New Point(0, 0)
            targetSize = New Size(176, 220)
        Else
            Dim aspectRatio As Double = CDbl(source.Width) / CDbl(source.Height)
            If aspectRatio >= 176.0 / 220.0 Then
                targetSize = New Size(176, CInt(176.0 / source.Width * source.Height))
            Else
                targetSize = New Size(CInt(220.0 / source.Height * source.Width), 220)
            End If
            position = New Point(CInt((176 - targetSize.Width) / 2), CInt((220 - targetSize.Height) / 2))
        End If

        Dim g As Graphics = Graphics.FromImage(output)
        g.InterpolationMode = opts.Quality
        g.FillRectangle(Brushes.Black, New Rectangle(0, 0, 176, 220))
        g.DrawImage(source, New Rectangle(position, targetSize))

        ' Save File
        Dim newFilePath As String = GetNewFilePath(fileInfo.FullName)
        Directory.CreateDirectory(Path.GetDirectoryName(newFilePath))
        output.Save(newFilePath, ImageFormat.Bmp)
    End Sub

    Shared Function GetNewFilePath(ByVal filePath As String) As String
        Dim fileName As String = Path.GetFileNameWithoutExtension(filePath) & ".bmp"
        Dim albumName As String = Path.GetDirectoryName(filePath)
        albumName = Path.GetFileName(albumName)
        Dim pathName As String

        If opts.CopyToPlayer Then
            pathName = Path.Combine(opts.PlayerPath, "PHOTO")
        Else
            pathName = opts.ExportPath
        End If

        Return Path.Combine(Path.Combine(pathName, albumName), fileName)
    End Function

    Shared Function GetImageList(ByVal imagesPath As String) As ArrayList
        Dim dir As New DirectoryInfo(imagesPath)
        Dim filelist As New ArrayList()
        filelist.AddRange(dir.GetFiles("*.jpg"))
        filelist.AddRange(dir.GetFiles("*.jpeg"))
        filelist.AddRange(dir.GetFiles("*.bmp"))
        filelist.AddRange(dir.GetFiles("*.gif"))
        filelist.AddRange(dir.GetFiles("*.png"))
        filelist.AddRange(dir.GetFiles("*.tif"))
        filelist.AddRange(dir.GetFiles("*.tiff"))
        filelist.AddRange(dir.GetFiles("*.emf"))
        filelist.AddRange(dir.GetFiles("*.ico"))
        filelist.AddRange(dir.GetFiles("*.wmf"))
        Return filelist
    End Function

#End Region

#Region " Events "

    Private Sub ProgressDialog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadAppSettings()
        Dim imgList As ArrayList = GetImageList(Me.PathToConvert)
        pbProgress.Maximum = imgList.Count
        pbProgress.Value = 0
        bgConverter.RunWorkerAsync(imgList)
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        If bgConverter.IsBusy Then
            If MessageBox.Show("Are you sure you want to cancel?", "Cancel", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
                bgConverter.CancelAsync()
                Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
                Me.Close()
            End If
        Else
            Me.DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()
        End If
    End Sub

    Private Sub bgConverter_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgConverter.DoWork
        Dim imgList As ArrayList = CType(e.Argument, ArrayList)
        index = 0
        For Each img As FileInfo In imgList
            index += 1
            filename = img.Name
            bgConverter.ReportProgress(index)
            ConvertImage(img)
        Next img
    End Sub

    Private Sub bgConverter_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgConverter.ProgressChanged
        lblStatus.Text = filename
        pbProgress.Value = index
    End Sub

    Private Sub bgConverter_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgConverter.RunWorkerCompleted
        lblStatus.Text = "Conversion Done."
        Cancel_Button.Text = "Close"
        My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep)
        Me.Close()
    End Sub

#End Region

End Class
